import { reactive } from "vue";
import { Api } from "../../../api/api.js";
import { ElNotification } from 'element-plus';
function userListJS() {
    let userinfo = JSON.parse(window.sessionStorage.getItem("userinfo"));
    let status = reactive({
        userinfo: userinfo,
        myselfLimit:userinfo.limit,
        userlist: [],
        findData:{
            limit:10,
            page:1,
            user:undefined,
            pages:"",
            total:""
        },
    })
    let methodFun = {
        //获取所有用户
        getUser: () => {
            Api.userFind(status.findData).then((res) => {
                status.findData.total = res.data.total;
                status.findData.pages = res.data.pages;
                status.findData.page = res.data.page;
                status.userlist.splice(0,status.userlist.length,...res.data.data);
            })
        },
        //禁用用户
        disableFun: (index, item) => {
            if (item.limit > status.myselfLimit) {
                item.disable = !item.disable;
                Api.datumChange(item).then((res) => {
                    if (res.data.disable === true) {
                        ElNotification({
                            type: "success",
                            title: "激活成功",
                            message: "用户已成功激活！",
                            duration: 1000,
                        });
                    } else {
                        ElNotification({
                            type: "warning",
                            title: "禁用成功",
                            message: "用户已成功禁用！",
                            duration: 1000,
                        });
                    }
                })
            } else {
                ElNotification({
                    type: "error",
                    title: "权限不足",
                    message: "无法禁用比自己高级或同级用户",
                    duration: 1000,
                });
            }
        },
        //删除用户
        removeFun:(index,item)=>{
            if(item.limit>status.myselfLimit){
                Api.removeUser(item).then((res)=>{
                    console.log(res.data);
                    if(res.data){
                        ElNotification({
                            type: "success",
                            title: "操作成功",
                            message: "用户已成功移除！",
                            duration: 1000,
                        });
                        methodFun.getUser();
                    }else{
                        ElNotification({
                            type: "error",
                            title: "操作异常",
                            message: "操作失败：发生未知错误！",
                            duration: 1000,
                        });
                   
                    }
                })
            }else{
                ElNotification({
                    type: "error",
                    title: "权限不足",
                    message: "无法禁用比自己高级或同级用户",
                    duration: 1000,
                });
            }
        },
        //改变页数
        changePage:(page)=>{
            status.findData.skip = page;
            methodFun.getUser();
        }
    }

    return {
        status,
        methodFun
    }
}

export default userListJS;